package com.design.softwaredesign.service.impl;

import com.design.softwaredesign.entity.Good;
import com.design.softwaredesign.entity.User;
import com.design.softwaredesign.front.UserInfo;
import com.design.softwaredesign.front.UserRegisterInfo;
import com.design.softwaredesign.mapper.UserMapper;
import com.design.softwaredesign.service.UserService;
import com.design.softwaredesign.vo.DataVo;
import com.design.softwaredesign.vo.GoodVo;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author :WaiterXiaoYY
 * @description: 用户业务逻辑层
 * @data :2020/10/11 18:25
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public DataVo userLogin(String phone, String password) {
        DataVo dataVo = new DataVo();
        User user = null;
        try {
            user = userMapper.selectUser(phone);
            if(user != null) {
                if(user.getPassword().equals(password)) {
                    List<User> users = new ArrayList<>();
                    users.add(user);
                    dataVo.setCode(200);
                    dataVo.setMsg("登录成功");
                    dataVo.setData(users);
                }
                else {
                    dataVo.setCode(202);
                    dataVo.setMsg("密码错误");
                }
            }else {
                dataVo.setCode(202);
                dataVo.setMsg("账户不存在");
            }
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("登录失败");
        } finally {
            return dataVo;
        }
    }

    @Override
    public int checkPhone(String phone) {
        DataVo dataVo = new DataVo();
        if(userMapper.checkPhone(phone) == 1) {
            dataVo.setCode(202);
            dataVo.setMsg("手机号已存在");
            return -1;
        }
        return 1;
    }

    @Override
    public DataVo userRegister(UserRegisterInfo userRegisterInfo) {
        DataVo dataVo = new DataVo();
        User user = new User();
        BeanUtils.copyProperties(userRegisterInfo, user);
        user.setUserId(UUID.randomUUID().toString());
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化当前系统日期
        String registerTime = dateFm.format(new java.util.Date());
        user.setUserRegisterTime(registerTime);
        user.setUserSex("保密");
        user.setUserSchool("广东财经大学");
        user.setUserState(1);

        try {
            userMapper.addUser(user);
            dataVo.setCode(200);
            dataVo.setMsg("注册成功");
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("注册失败");
            e.printStackTrace();

        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo userTest() {
        DataVo dataVo = new DataVo();
        String password = "a123";
        User user = userMapper.selectUser(password);
        System.out.println(user);
        return dataVo;
    }

    @Override
    public int updateCode(String phone, String code) {
        try {
            if (userMapper.checkCodePhone(phone) == 1) {
                userMapper.updateCode(phone, code);
                return 1;
            } else {
                userMapper.addCodePhone(phone, code);
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public String getPhoneCode(String phone) {
        return userMapper.getPhoneCode(phone);
    }


    // next code by freaks

    @Override
    public DataVo userUpdateInfo(UserInfo userInfo) {
        DataVo dataVo = new DataVo();
        User user = userMapper.selectUser(userInfo.getPhone());;
        try {
            if (userInfo != null){
                BeanUtils.copyProperties(userInfo,user);
//                user = userMapper.selectUser(userInfo.getPhone());
//                user.setUserName(userInfo.getUserName());
//                user.setPassword(userInfo.getPassword());
//                user.setUserSex(userInfo.getUserSex());
//                user.setUserAddress(userInfo.getUserAddress());
//                user.setUserSchool(userInfo.getUserSchool());
//                user.setUserWechat(userInfo.getUserWechat());
//                user.setUserEmail(userInfo.getUserEmail());
            }
            List<User> userList = new ArrayList<User>();
            userMapper.updateUserInfo(userInfo);
            userList.add(user);
            dataVo.setData(userList);
            dataVo.setCode(200);
            dataVo.setMsg("修改成功");
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("修改失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getUserInfoByUserId(String userId) {
        DataVo dataVo = new DataVo();
        User user = new User();
        try{
            if (userId != null) {
                UserInfo userInfo = userMapper.selectUserByUserId(userId);
                BeanUtils.copyProperties(userInfo,user);
            }
            else throw new Exception("id为空！");
            List<User> userList = new ArrayList<>();
            userList.add(user);
            dataVo.setData(userList);
            dataVo.setCode(200);
            dataVo.setMsg("查询成功");
        }catch (Exception e)
        {
            dataVo.setCode(202);
            dataVo.setMsg("查询失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getUserInfoByPhone(String phone) {
        DataVo dataVo = new DataVo();
        User user = new User();
        try{
            if (phone != null) {
                UserInfo userInfo = userMapper.selectUserByPhone(phone);
                BeanUtils.copyProperties(userInfo,user);
            }
            else throw new Exception("电话号码为空！");
            List<User> userList = new ArrayList<>();
            userList.add(user);
            dataVo.setData(userList);
            dataVo.setCode(200);
            dataVo.setMsg("查询成功");
        }catch (Exception e)
        {
            dataVo.setCode(202);
            dataVo.setMsg("查询失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getMyGoods(String userId, Integer pageNum, Integer pageSize, String query) {
        DataVo dataVo = new DataVo();
        List<Good> goodslist = null;
        try {
            PageHelper.startPage(pageNum, pageSize);
            goodslist = userMapper.getMyGoods(userId,query);
            PageInfo<Good> goodPageInfo = new PageInfo<>(goodslist);
            List<GoodVo> goodVoList = new ArrayList<>();
            Good good = null;
            for(int i = 0; i < goodslist.size(); i++) {
                GoodVo goodVo = new GoodVo();
                good = goodslist.get(i);
                BeanUtils.copyProperties(good, goodVo);
                if(!good.getImageUrl().equals("")) {
                    String []imageUrls = good.getImageUrl().split(";");
                    List<String> imageUrlList = new ArrayList<>();
                    for(String imageUrl : imageUrls) {
                        imageUrlList.add(imageUrl);
                    }
                    goodVo.setImageUrlList(imageUrlList);
                }
                goodVoList.add(goodVo);
            }
            dataVo.setCode(200);
            dataVo.setCount(goodPageInfo.getTotal());
            dataVo.setMsg("查询我的闲置成功");
            dataVo.setData(goodVoList);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("查询我的闲置失败");
            e.printStackTrace();
        } finally {
            return  dataVo;
        }
    }


    @Override
    public DataVo getMyTransactions(String userId, Integer pageNum, Integer pageSize, String query) {
        DataVo dataVo = new DataVo();
        List<Good> goodslist = null;
        try {
            PageHelper.startPage(pageNum, pageSize);
            goodslist = userMapper.getMyTransactions(userId,query);
            PageInfo<Good> goodPageInfo = new PageInfo<>(goodslist);
            List<GoodVo> goodVoList = new ArrayList<>();
            Good good = null;
            for(int i = 0; i < goodslist.size(); i++) {
                GoodVo goodVo = new GoodVo();
                good = goodslist.get(i);
                BeanUtils.copyProperties(good, goodVo);
                if(!good.getImageUrl().equals("")) {
                    String []imageUrls = good.getImageUrl().split(";");
                    List<String> imageUrlList = new ArrayList<>();
                    for(String imageUrl : imageUrls) {
                        imageUrlList.add(imageUrl);
                    }
                    goodVo.setImageUrlList(imageUrlList);
                }
                goodVoList.add(goodVo);
            }
            dataVo.setCode(200);
            dataVo.setCount(goodPageInfo.getTotal());
            dataVo.setMsg("查询我的订单成功");
            dataVo.setData(goodVoList);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("查询我的订单失败");
            e.printStackTrace();
        } finally {
            return  dataVo;
        }

    }
}

